package cn.itsource.hrm.service.impl;

import cn.itsource.hrm.domain.Employee;
import cn.itsource.hrm.domain.Tenant;
import cn.itsource.hrm.domain.dto.LoginUserInfo;
import cn.itsource.hrm.mapper.EmployeeMapper;
import cn.itsource.hrm.mapper.PermissionMapper;
import cn.itsource.hrm.mapper.TenantMapper;
import cn.itsource.hrm.service.IEmployeeService;
import cn.itsource.hrm.util.AssertUtils;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ¿­
 * @since 2021-01-15
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Autowired
    private TenantMapper tenantMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public boolean isexistUserName(String username) {
        return baseMapper.isexistUserName(username);

    }

    @Override
    public LoginUserInfo selectUserInfo(Long loginId) {
        //1.判断一下参数
        AssertUtils.isnotNull("用户登录信息不足",loginId);

        //2.查询employee对象
        Employee employee =  baseMapper.selectByLoginID(loginId);

        // 3.查询机构信息
        Tenant tenant = tenantMapper.selectById(employee.getTenantId());
        // 4. 用户的权限
        List<String> permis = permissionMapper.selectPerimssByUserId(employee.getId());

        LoginUserInfo loginUserInfo = new LoginUserInfo(
                loginId,employee.getId(),employee.getUsername(),tenant.getId()
                ,tenant.getCompanyName(),permis);

        return loginUserInfo;

    }
}
